<script setup lang="ts">
  import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
  import { FormInstance } from 'element-plus'
  import { reqForceLogout, reqOnlinelist } from '@/api/monitor/online'
  import { reqTableData } from '@/api/type'
  import { parseTime } from '@/utils'

  const { proxy }: any = getCurrentInstance()
  const total = ref(0)
  const loading = ref(true)
  const tableData = ref([])
  const ruleFormRef = ref<FormInstance>()
  const queryParams = reactive({
    ipaddr: undefined,
    userName: undefined,
  })
  const pageNum = ref(1)
  const pageSize = ref(10)

  const getList = async () => {
    loading.value = true

    await reqOnlinelist(queryParams).then((req: reqTableData) => {
      if (req.code === 200) {
        tableData.value = req.rows
        total.value = req.total
      } else {
        proxy.$modal.msgError('获取列表失败！')
      }
    })
    setTimeout(() => {
      loading.value = false
    }, 500)
  }

  // 搜索
  const handleQuery = () => {
    pageNum.value = 1
    getList()
  }

  // 重置
  const resetQuery = () => {
    ruleFormRef.value.resetFields()
    Object.keys(queryParams).forEach((key) => {
      queryParams[key] = undefined
    })
    pageNum.value = 1
    pageSize.value = 10
  }

  // 强退用户
  const handleForceLogout = (row: any) => {
    proxy.$modal
      .confirm('是否确认强退名称为"' + row.userName + '"的用户？')
      .then(function () {
        return reqForceLogout(row.tokenId)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess('强退成功')
      })
      .catch(() => {})
  }

  onMounted(() => {
    handleQuery()
  })
</script>

<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" :model="queryParams" ref="ruleFormRef">
        <el-form-item label="登录地址" prop="ipaddr">
          <el-input
            v-model="queryParams.ipaddr"
            placeholder="请输入登录地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <div class="util"> </div>
      <div class="table-inner">
        <el-table
          v-loading="loading"
          :data="tableData.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
          style="width: 100%"
          border
        >
          <el-table-column label="序号" width="55" type="index" align="center">
            <template #default="scope">
              <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="会话编号"
            align="center"
            prop="tokenId"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="登录名称"
            align="center"
            prop="userName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="部门名称" align="center" prop="deptName" />
          <el-table-column
            label="主机"
            align="center"
            prop="ipaddr"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="登录地点"
            align="center"
            prop="loginLocation"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="浏览器" align="center" prop="browser" />
          <el-table-column label="操作系统" align="center" prop="os" />
          <el-table-column label="登录时间" align="center" prop="loginTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.loginTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            v-hasPermi="['monitor:online:forceLogout']"
          >
            <template #default="scope">
              <el-button
                type="primary"
                icon="Delete"
                size="small"
                @click="handleForceLogout(scope.row)"
                v-hasPermi="['monitor:online:forceLogout']"
                >强退</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <!--分页器            -->
        <el-pagination
          v-show="total > 0"
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          :background="true"
          layout="prev, pager, next, jumper, ->,  sizes, total"
          :total="total"
          :limit.sync="pageSize"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
  @import './index';
</style>
